//
//  LXUserLoginCell.m
//  cat
//
//  Created by imera on 16/3/17.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXUserLoginCell.h"
#import "UIImage+Lx.h"

#define LXUserLoginCellViewMargin 10

@interface LXUserLoginCell()
/** 用户头像 */
@property (nonatomic, weak) UIImageView *userPhotoImageView;
/** 用户姓名 */
@property (nonatomic, weak) UILabel *userNameLabel;
/** 登录按钮 */
@property (nonatomic, weak) UIButton *loginBtn;
@end

@implementation LXUserLoginCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        [self initSubViews];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

#pragma mark - 初始化子控件
- (void)initSubViews
{
    UIImageView *userPhotoImageView = [[UIImageView alloc]init];
    self.userPhotoImageView = userPhotoImageView;
    [self addSubview:userPhotoImageView];
    
    UILabel *userNameLabel = [[UILabel alloc]init];
    self.userNameLabel = userNameLabel;
    [self addSubview:userNameLabel];
    
    UIButton *loginBtn = [[UIButton alloc]init];
    self.loginBtn = loginBtn;
    [self addSubview:loginBtn];
}

#pragma mark - 设置子控件数据
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    /** 用户头像 */
    UIImageView *userPhotoImageView = self.userPhotoImageView;
    [userPhotoImageView setImage:[UIImage imageNamed:@"userPhoto"]];
    
    CGFloat userPhotoImageViewX = LXUserLoginCellViewMargin;
    CGFloat userPhotoImageViewY = LXUserLoginCellViewMargin * 0.5;
    CGFloat userPhotoImageViewW = 70;
    CGFloat userPhotoImageViewH = (self.frame.size.height - userPhotoImageViewY * 2);
    userPhotoImageView.frame = CGRectMake(userPhotoImageViewX, userPhotoImageViewY, userPhotoImageViewW, userPhotoImageViewH);
    
    //设置圆角
    userPhotoImageView.layer.masksToBounds = YES;
    userPhotoImageView.layer.cornerRadius = userPhotoImageViewW * 0.5;
    userPhotoImageView.layer.borderWidth = 2;
    userPhotoImageView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    /** 用户姓名 */
    UILabel *userNameLabel = self.userNameLabel;
    userNameLabel.text = @"用户未登录";
    userNameLabel.font = [UIFont systemFontOfSize:15];
    
    CGSize userNameLabelSize = [userNameLabel.text sizeWithAttributes:@{NSFontAttributeName:userNameLabel.font}];
    CGFloat userNameLabelX = CGRectGetMaxX(userPhotoImageView.frame) + LXUserLoginCellViewMargin;
    CGFloat userNameLabelY = userPhotoImageViewY + LXUserLoginCellViewMargin;
    CGFloat userNameLabelW = userNameLabelSize.width;
    CGFloat userNameLabelH = userNameLabelSize.height;
    userNameLabel.frame = CGRectMake(userNameLabelX, userNameLabelY, userNameLabelW, userNameLabelH);
    
    /** 登录按钮 */
    UIButton *loginBtn = self.loginBtn;
    [loginBtn setBackgroundImage:[UIImage resizeWithName:@"button-blue"] forState:UIControlStateNormal];
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(userLogin) forControlEvents:UIControlEventTouchUpInside];
    
    CGFloat loginBtnX = userNameLabelX;
    CGFloat loginBtnY = CGRectGetMaxY(userNameLabel.frame) + 10;
    CGFloat loginBtnW = 80;
    CGFloat loginBtnH = 25;
    loginBtn.frame = CGRectMake(loginBtnX, loginBtnY, loginBtnW, loginBtnH);
}

- (void)userLogin
{
    if ([self.MyDelegate respondsToSelector:@selector(login:)]) {
        [self.MyDelegate login:self];
    }
}
@end
